// Copyright 2008 web2me.dev
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.googlecode.web2me.plugins;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

import org.apache.log4j.Logger;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Jars a project with more flexibility
 * @author web2me
 *
 * @goal jar
 *
 * @phase compile
 *
 */
public class JarMojo extends AbstractMojo {

	/**
	 * The MANIFEST.MF file to include int the JAR
	 * @parameter expresion=${jar.manifest} default-value="resources/MANIFEST.MF"
	 * @required
	 */
	private File manifest;
	
	/**
	 * The JAR file path that is going to be generated
	 * @parameter expresion=${jar.jar}
	 * @required
	 */
	private File destinyJar;
	
	private Logger logger = Logger.getLogger(JarMojo.class);
	
	public void execute() throws MojoExecutionException, MojoFailureException {
		OutputStream outputJar = null;
		JarOutputStream jar = null;
		
		try {
			outputJar = new FileOutputStream(destinyJar);
			jar = new JarOutputStream(outputJar, new Manifest(new FileInputStream(manifest)));
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException caught", e);
			throw new MojoExecutionException("FileNotFoundException caught", e);
		} catch (IOException e) {
			logger.error("IOException caught", e);
			throw new MojoExecutionException("IOException caught", e);
		}
		
		
	}

}
